package com.db.logger.service;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class ConfigService {
    private static Properties properties = readProperties();

    private static final String CONFIG_PATH_PROPERTY = "LoggerConfig";

    public static String getProperty(String name, String property) {
        return properties.getProperty(name + "." + property);
    }

    private static Properties readProperties() {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream(readConfigPath()));
        } catch (IOException e) {
            throw new RuntimeException("Can not load config");
        }
        return properties;
    }

    private static String readConfigPath() {
        String configPath = System.getProperty(CONFIG_PATH_PROPERTY);
        if (configPath != null) {
            return configPath;
        } else {
            throw new RuntimeException("Can not get config path (You can specify it by JVM option -D" + CONFIG_PATH_PROPERTY + ")");
        }
    }
}
